
var api = require('../../utils/api.js');
var app = getApp();
Page({
    data: {
        tophe:0,
        mobile:'',
        code:'',
        title:'获取验证码'
    },
    currentPageNumber: 1,
        onLoad() {
        var file=api.FIlE;
        this.setData({
            bg:file
        })
    
    },
    onShow() {
        let jn = wx.getMenuButtonBoundingClientRect();

        console.log(jn);
        var tophe=jn.top+jn.height;
        var over =tophe+230 ;
        this.setData({
            tophe:tophe,
            over:over+ 'px'
        })
     
    },
    getmobile(ret){
        this.setData({
            mobile:ret.detail.value
        })
    },
    getcode(ret){
        this.setData({
            code:ret.detail.value
        })
    },
    sendsms(){
        var mobile=this.data.mobile;
        var title=this.data.title;
        var that=this;


        if(title!='获取验证码'){
            return false;
        }

        api.post({
            url: 'sms/send',
            data: {mobile:mobile},
            success: data => {
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })

                if(data.code==1){
                    that.outime();
                }              
            },
        });
    },
    outime(){
        var that=this;
        var i=60;
        var index=setInterval(() => {

            console.log(i)
            i=i-1;
            that.setData({
                title:i +' s 后获取'
            })

            if(i<1){
                this.setData({
                    title:'获取验证码'
                })
                clearInterval(index);
            }
        }, 1000);
    },
    loginfor(){
        var that=this;

        var mobile=this.data.mobile;
        var code=this.data.code;

        api.post({
            url: 'user/mobilelogin',
            data: {mobile:mobile,captcha:code},
            success: data => {
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })

                wx.setStorageSync('token', data.data.userinfo.token)
                wx.setStorageSync('user', data.data.userinfo)      

                wx.switchTab({
                  url: '/pages/index/index',
                })
            },
        });


    }
});
